/*
 * RTSAI - Real-Time Strategy engine for AI research
 * Copyright(C) 2006 DIMAp-UFRN, licensed under LGPL
 *
 * Interface para a linguagem script Lua
 */

#ifndef __CPPLUA_H
#define __CPPLUA_H

extern "C" {
    #include "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"
}

#include <iostream>
#include <string>
#include "tolua++.h"

int luaopen_rtsai (lua_State* tolua_S);

/*! This class implements an Interface between C++ and Lua
 */
class Lua {
    static lua_State *L;

private:
    void openlibs() {
        luaL_openlibs(L);
        luaopen_rtsai(L);
        runfile("scripts/rtsai.lua");
    }
    static int push_stack(const std::string& var);
    static int push_stack_set(const std::string& var, int &index);
    static void call_function_error(const char *fmt, ...);

public:
    /** No construtor, o descritor de estado de Lua e suas libs sao inicializados */
    Lua();
    Lua(const char *filename);
    ~Lua();

    static void reset();
    static void runfile (const char *filename);
    static int execute();
    inline operator lua_State*();
    static void* getVar(char *varname);
    static int get_integer(const std::string& var);
    static void set_integer(const std::string& var, int value);
    static const char* get_string(const std::string& var);
    static void set_string(const std::string& var, const char *value);
    static void call_function(const std::string& func, const char *sig, ...);
};

#endif // __CPPLUA_H
